import Taro from '@tarojs/taro';
//选择文件
export const chooseFile = function (option) {
    const {count = 1, success, fail, complete} = option
    const res: any = {
        errMsg: 'chooseFile:ok',
        tempFilePaths: [],
        tempFiles: []
    }
    if (count && typeof count !== 'number') {
        res.errMsg = 'error,typeof count is error',
            console.error(res.errMsg)
        typeof fail === 'function' && fail(res)
        typeof complete === 'function' && complete(res)
        return Promise.reject(res)
    }

    if (Taro.getEnv() === Taro.ENV_TYPE.WEB) {
        const fileId = "MyUtilsChooseFile"
        let taroChooseImageId: any = document.getElementById(fileId)
        if (!taroChooseImageId) {
            let htmlInputElement: any = document.createElement("input");
            htmlInputElement.type = "file"
            htmlInputElement.setAttribute('id', fileId)
            htmlInputElement.setAttribute('style', 'position: fixed; top: -4000px; left: -3000px; z-index: -300;')
            if (count > 1) {
                htmlInputElement.setAttribute('multiple', 'multiple')
            }
            document.body.appendChild(htmlInputElement)
            taroChooseImageId = document.getElementById(fileId)
        } else {
            if (count > 1) {
                taroChooseImageId.setAttribute('multiple', 'multiple')
            } else {
                taroChooseImageId.removeAttribute("multiple")
            }
        }

        // htmlInputElement.accept = "application/*"
        // htmlInputElement.accept = "application/pdf,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        // htmlInputElement.accept = "image/jpeg,image/gif,image/png"

        taroChooseImageId.onchange = (e) => {
            // console.log("onchange",e)
            let arr = [...e.target.files]
            arr = arr.splice(0, count)
            arr && arr.forEach(item => {
                let blob = new Blob([item], {
                    type: e.target.files[0].type
                });
                let url = URL.createObjectURL(blob);
                res.tempFilePaths.push(url)
                // res.tempFiles.push({path: url, size: item.size, type: item.type, originalFileObj: item})
                console.log("选择item", item)
                res.tempFiles.push({
                    path: url,
                    size: item.size,
                    type: item.type,
                    name: item.name,
                    lastModified: item.lastModified,
                    lastModifiedDate: item.lastModifiedDate,
                    webkitRelativePath: item.webkitRelativePath
                })
            })
            typeof success === "function" && success(res)
            typeof complete === "function" && complete(res)

            e.target.value = ''
            // document.body.removeChild(taroChooseImageId)
        }
        //触发选择
        taroChooseImageId.click()

    } else {
        Taro.chooseMessageFile({
            count: count,
            type: 'file',
            success: function (res) {
                // console.log("选择文件",res)
                success ? success(res) : ""
            }
        })
    }
}
